$(function () {
    localStorage.removeItem("dataType");
    setNewsTypeFindAll();
    setNewsFindAll();
    let newstype=localStorage.getItem("newstype");
    if (newstype!=null){
        setNewsType(newstype);
        localStorage.removeItem("newstype");
    }
})

//查询所有新闻
function setNewsFindAll() {
    let data=myAjax("/News","","get");
    data=data.newsNimitFind;
    setNewsfind(data);
}

//根据id查询新闻详细
function selectNewsById(id) {
    window.open("newsDetailed.html","_self");
    localStorage.setItem("NewsId",id);
}

//获得新闻类型
function setNewsTypeFindAll() {
    let data=myAjax("/News","","get");
    data=(data.NewsTypeFindAll);
    let html='<div><a href="home.html">首页</a> >> <a href="about.html">关于我们</a></div>';
    for (let i=0;i<data.length;i++){
        html +='<div class="product" onclick="setNewsType(\''+data[i].newstype+'\')"><p>'+data[i].newstype+'</p></div>';
    }
    $("#NewsType").html(html);
}

//根据新闻类型查询新闻
function setNewsType(data) {
    localStorage.setItem("dataType",data);
    let da={
        newstype:data,
    }
    let newstype=myAjax("/News",da,"get");
    newstype=newstype.newstypeFind;
    setNewsfind(newstype);
}

var i=1;
//上一页
function newsfrist(){
    let dataType= localStorage.getItem("dataType");
    if (i>1) {
        let da={
            newstype:dataType,
            page:--i,
        }
        let data=myAjax("/News",da,"get");
        data=data.newsNimitFind;
        setNewsfind(data);
    }
}
//下一页
function newsnext() {
    i=++i;
    let dataType= localStorage.getItem("dataType");
    let da={
        newstype:dataType,
        page:i,
    }
    let data=myAjax("/News",da,"get");
    let number=data.number;
    data=data.newsNimitFind;
    let pa=i-1;
    number=Math.ceil(number/6);
    if (number>pa){
        setNewsfind(data);
    }else {
        --i;
    }
}


function setNewsfind(data) {
    let html='';
    for (let i=0;i<data.length;i++){
        html +='<div>\n' +
            '                <div>\n' +
            '                    <p></p>\n' +
            '                    <p>'+data[i].date+'</p>\n' +
            '                </div>\n' +
            '                <div>\n' +
            '                    <p class="product"  onclick="selectNewsById('+data[i].id+')">'+data[i].title+'</p>\n' +
            '                    <p>'+data[i].content+'</p>\n' +
            '                </div>\n' +
            '            </div>';
    }
    $('#newsFindAll').html(html);
}
